#ifndef _XBEE_H_
#define _XBEE_H_

#ifndef XBEE_BAUD
#define XBEE_BAUD 9600
#endif
#define SERIAL0_BAUD XBEE_BAUD

#include <types/anos_error.h>
#include <serial0/serial0.h>

#define ANOS_DATA_LENGTH 100

#ifndef XBEE_MAC_SRC_ADD
#define XBEE_MAC_SRC_ADD 0
#endif

#define MAC_MODE	 	2 


//PUBLIC
	typedef struct{
		uint8_t src[2];
		uint8_t dest[2];
		uint8_t len[2];
		uint8_t seq;
		uint8_t type;
		uint8_t hop;
	}msg_header_t;

	typedef struct{
	}msg_footer_t;

	typedef struct{
		uint8_t rssi;
	}msg_metadata_t;

	typedef struct{
		uint8_t header[sizeof(msg_header_t)];
		uint8_t payload[ANOS_DATA_LENGTH-sizeof(msg_header_t)];
		uint8_t footer[sizeof(msg_footer_t)];
		uint8_t metadata[sizeof(msg_metadata_t)];
	}message_t;
	
	//Data Field:
	typedef struct {
		uint8_t delimiter;
		uint8_t len[2];
		uint8_t id;
	}atHeader_t;

	typedef struct {
		uint8_t frameId;
		uint8_t command[2];
	}atCommandWR_t;

	typedef struct{
		uint8_t frameId;
		uint8_t command[2];
		uint8_t value;
	}atCommandMM_t;

	typedef struct {
		uint8_t frameId;
		uint8_t command[2];
		uint8_t value[2];
	}atCommandMY_t;

	typedef struct{
		uint8_t frameId;
		uint8_t command[2];
		uint8_t status;
		uint8_t value[2];	
	}atResponse_t;

	typedef struct {
		uint8_t frameId;
		uint8_t dest[2];
		uint8_t option;
		uint8_t payload[100];
	}rfTransmit_t;

	typedef struct {
		uint8_t frameId;
		uint8_t status;
	}rfTransmitS_t;

	typedef struct {
		uint8_t src[2];
		uint8_t rssi;
		uint8_t option;
		uint8_t payload[100];
	}rfReceive_t;

	typedef union {
		atCommandWR_t atCommandWR;
		atCommandMM_t atCommandMM;
		atCommandMM_t atCommandMY;
		atResponse_t atResponse;
		rfTransmit_t rfTransmit;
		rfTransmitS_t rfTransmitS;
		rfReceive_t rfReceive;
	}atCommandPayload_t;

	typedef struct {
		uint8_t header[sizeof(atHeader_t)];
		uint8_t payload[sizeof(atCommandPayload_t)];
		uint8_t checksum;
	}atCommand_t;

	//Function Field:
	error_t XBEE_init(void);
	error_t XBEE_start(void);
	error_t XBEE_send(uint16_t dest_add, void* msg,uint8_t len);

	void XBEE_reg(void(*)(error_t),void(*)(error_t),message_t *(*)(message_t*,void*,uint8_t));

#endif
